# coding: utf-8
from Tkinter import *
from ttk import *


class EasyText(Text):
    def __init__(self, parent, **kw):
        self.parent = parent
        # 文本框与滚动条
        self.text_sv = Scrollbar(self.parent, orient=VERTICAL)  # 文本框-竖向滚动条
        self.text_sh = Scrollbar(self.parent, orient=HORIZONTAL)  # 文本框-横向滚动条
        Text.__init__(self, self.parent, yscrollcommand=self.text_sv.set,
                      xscrollcommand=self.text_sh.set, wrap='none', **kw)
        # self.memo = Text(self.parent, height=15, yscrollcommand=self.text_sv.set,
        #                  xscrollcommand=self.text_sh.set, wrap='none')

        # 滚动事件
        self.text_sv.config(command=self.yview)
        self.text_sh.config(command=self.xview)
        self.text_sv.pack(fill="y", expand=0, side=RIGHT, anchor=N)
        self.text_sh.pack(fill="x", expand=0, side=BOTTOM, anchor=N)
        self.pack(side=TOP, fill=BOTH, expand=True)

    def append_text(self, new_text):
        try:
            if int(self.index(END).split(".")[0]) > 8000:
                self.clear_text()
            self.insert(END, new_text, )
        except Exception as e:
            print "insert fail, try again:", e
            if int(self.index(END).split(".")[0]) > 8000:
                self.clear_text()
            self.insert(END, new_text, )

    def clear_text(self):
        self.delete(0.0, END)

    def set_warp(self, is_wrap=False):
        if is_wrap:
            self["wrap"] = "word"
        else:
            self["wrap"] = "none"


class LabelEntry(Entry):
    def __init__(self, parent, label,
                 default_text="", blanking=" : ",
                 side=LEFT, width=16, **kw):
        self.parent = parent
        self.var_text = StringVar()
        self.var_text.set(default_text)

        self.frame = Frame(self.parent)

        Label(self.frame, text=label+blanking).pack(side=LEFT, pady=4)
        Entry.__init__(self, self.frame, textvariable=self.var_text, width=width, **kw)
        self.pack(side=LEFT)
        # Entry(self.frame, textvariable=self.var_text, width=width).pack(side=LEFT)
        self.frame.pack(side=side, padx=4, pady=4)

# class LabelEntry:
#     def __init__(self, parent, label,
#                  default_text="", blanking=" : ",
#                  side=LEFT, width=10):
#         self.parent = parent
#         self.var_text = StringVar()
#         self.var_text.set(default_text)
#
#         self.frame = Frame(self.parent)
#
#         Label(self.frame, text=label+blanking).pack(side=LEFT)
#         Entry(self.frame, textvariable=self.var_text, width=width).pack(side=LEFT)
#         self.frame.pack(side=side, padx=4)

    def get_text(self):
        return self.var_text.get()

    def set_text(self, text):
        return self.var_text.set(text)


class LabelCombobox(Combobox):
    def __init__(self, parent, label, values, command, default_value="", **kw):
        Combobox.__init__(self, parent, values=values, **kw)

        Label(parent, text=label).pack(side=LEFT, pady=4)
        # self.cbb_logtype = self
        # self.cbb_logtype["state"] = "readonly"
        # self.cbb_logtype["values"] = self.logtypes
        self.bind("<<ComboboxSelected>>", command)
        if default_value in values:
            self.set(default_value)
        else:
            self.current(0)
        self.pack(side=LEFT, padx=4, pady=4)

        # 自定义控件等效于以下代码
        # Label(frm_input, text="logtype : ").pack(side=LEFT, pady=4)
        # self.cbb_logtype = Combobox(frm_input, state="readonly", values=self.logtypes)
        # # self.cbb_logtype["state"] = "readonly"
        # # self.cbb_logtype["values"] = self.logtypes
        # self.cbb_logtype.bind("<<ComboboxSelected>>", self.select_logtype)
        # self.cbb_logtype.current(0)
        # self.cbb_logtype.pack(side=LEFT, padx=4, pady=4)


class LabelSpinbox:
    def __init__(self, parent, label, textvariable, entry="", width=10):
        self.parent = parent
        self.textvariable = textvariable
        self.content = StringVar()
        self.content.set(entry)

        self.frame = Frame(self.parent, height=120)

        Label(self.frame, text=label+" : ", width=10).pack(side=LEFT)
        Spinbox(self.frame, textvariable=textvariable, width=9, increment=10000, from_=100, to=1000000).pack(side=LEFT)
        self.frame.pack()

    def get_entry(self):
        return self.content.get()


class UICheckbutton(Checkbutton):
    def __init__(self, parent, checked=0, **kw):
        Checkbutton.__init__(self, parent, **kw)
        # super(UICheckbutton, self).__init__(master=parent, kw)
        self.var_check = IntVar(value=checked)
        self["variable"] = self.var_check
        self.pack(side=LEFT, padx=4, pady=4)

        # 自定义控件等效于以下代码
        # self.var_has_f = IntVar()
        # Checkbutton(frm_input, text="含有f", variable=self.var_has_f).pack(side=LEFT, padx=4, pady=4)

    def get_checked(self):
        return self.var_check.get()
